package com.lmb.todo.dao;

import com.lmb.todo.pojo.Group;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author lmb
 */
@Mapper
public interface GroupDao {

    /**
     * 获取指定 userId 的所有分组
     * @param userId Integer
     * @return List<Group>
     */
    @Select("select id, userId, folderId, count, icon, gname, descr, createTime, updateTime " +
            "from t_group " +
            "where userId=#{userId}")
    List<Group> getAllByUserId(Integer userId);

    /**
     * 更改分组数据
     * @param group Group
     * @return Integer
     */
    @Update("<script>" +
            "update t_group " +
            "<set>" +
            "<if test='folderId != null'> folderId = #{folderId}, </if>" +
            "<if test='icon != null'> icon = #{icon}, </if>" +
            "<if test='gname != null'> gname = #{gname}, </if>" +
            "<if test='descr != null'> descr = #{descr}, </if>" +
            "<if test='createTime != null'> createTime = #{createTime}, </if>" +
            "<if test='updateTime != null'> updateTime = #{updateTime}, </if>" +
            "<if test='count != null'> count = #{count} </if>" +
            "</set>" +
            "where id=#{id}" +
            "</script>")
    Integer update(Group group);

    /**
     * 根据分组id删除分组
     * @param id Integer
     * @return Integer
     * */
    @Delete("delete from t_group where id=#{id}")
    Integer delete(Integer id);

     /**
     * 根据id更改分组folderId
     * @param folderId Integer
     * @param id Integer
     * @return Integer
     * */
     @Update("update t_group set folderId=#{folderId} where id=#{id}")
     Integer move(Integer folderId, Integer id);

    /**
     * 新增分组
     * @param group Group
     * @return Integer
     * */
     @Insert("insert into t_group(userId, folderId, icon, gname, descr, createTime, updateTime, count)" +
             "values(#{userId}, #{folderId}, #{icon} , #{gname}, #{descr}, #{createTime}, null, 0)")
     @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
     Integer add(Group group);

    /**
     * 根据t_todo的groupId删除t_todo
     * @param groupId Integer
     * @return Integer
     * */
    @Delete("delete from t_todo where groupId=#{groupId}")
    Integer deleteTodo(Integer groupId);

    /**
    * 修改 count
    * @param id Integer
    * @param count Integer
    * @param type String 定义操作类型，add：增加，minus：减少，equal：等于
    * @return Integer
    * */
    @Update("<script> " +
            "update t_group set " +
            "<if test='type == \"add\"'> count=count+#{count} </if>" +
            "<if test='type == \"minus\"'> count=count-#{count} </if>" +
            "<if test='type == \"equal\"'> count=#{count} </if>" +
            "where id=#{id} " +
            "</script> ")
    Integer count(Integer id, Integer count, String type);

    /**
    * 根据 userId 查找 groupId 集合
    * @param userId Integer
    * @return List<Integer>
    * */
    @Select("select id from t_group where userId=#{userId}")
    List<Integer> getGroupIdsByUserId(Integer userId);
}